import { toRefs } from 'vue'
import { useRolesStore } from '@/store/roles'

const role = {
  async mounted(el, binding, vnode) {
    const rolesStore = useRolesStore()
    const { roles } = toRefs(rolesStore)
    const { value } = binding

    if (roles.value.length === 0) await rolesStore.initRoles()

    if (roles.value.includes('admin')) return
    else {
      if (Array.isArray(value)) {
        if (!roles.value.some((v) => value.includes(v))) el.remove()
      } else {
        if (!roles.value.includes(value)) el.remove()
      }
    }
  }
}

export default role
